<!--
 * @Author: your name
 * @Date: 2021-04-30 11:52:56
 * @LastEditTime: 2021-05-27 12:12:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\overview\co2Modal.vue
-->
<template>
    <div>
        <el-dialog
        :title="$t('savings.factor')"
        :visible="visible"
        width="700px"
        @close="$emit('update:visible', false)"
        :close-on-click-modal='false'
        >
            <div>
                <baseForm :option='option' v-model="form" ref='child'>
                    <template #nice='{data}'>
                        <el-input-number size="mini" v-model="data.form[data.item.name]" :precision="3" :step="0.001" :min='0' :max='1'></el-input-number>
                    </template>
                </baseForm>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="$emit('update:visible', false)">{{$t('cancel')}}</el-button>
                <el-button :loading="loading"  type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseForm from '../../components/common/baseForm'
export default {
    components: {
        baseForm
    },
    props: {
        visible:{
            type:Boolean,
            default:false
        },
    },
    data() {
        return {
            loading:false,
            form:{},
            value:''
        };
    },
    computed: {
        option(){
            return {
                columnNum:'1',
                offset:3,
                labelWidth:'250px',
                data:[{
                    type:'slot',
                    slot:'nice',
                    lable:this.$t('savings.factor'),
                    name:'co2Factor',
                    value:this.value,
                    rules:[{ required: true, message: '不能为空'}]
                }
            ],
               
        }}
    },
    watch: {
        visible:{
            handler(nv,ov){
                if(nv){
                    this.getSiteData()
                }
            },
        }
    },
    created() {

    },
    mounted() {

    },
    methods: {
        async getSiteData(){
            let res = await this.API.getSiteData({
                id:this.$route.query.siteId
            })
            if(res){
              this.value = res.co2Factor  
            }
        },
        async save(){
            if(!this.$refs.child.save()){
                return
            }
            this.loading = true
            let res = await this.API.siteSaveCo2Factor({
                siteId:this.$route.query.siteId,
                co2Factor:this.form.co2Factor,
            })
            this.loading = false
            if(this.comm.showMsg(res.success)){
                this.$emit('update:visible', false)
                this.$emit('uploadCo2Data', true)
            }
        }
    },
};
</script>

<style  lang="less" scoped>

</style>
